package com.shujia.scala

import scala.io.Source

object Demo11WordCount {
  def main(args: Array[String]): Unit = {
    /**
      * wordcount
      * 1、读取文件
      *
      */

    val lines = Source.fromFile("java/data/words.txt").getLines()

    //将所有单词拆分出来
    val words = lines.flatMap(line => line.split(","))


    //通过单词进行分组
    val groupList = words.toList.groupBy(word => word)

    val countList = groupList.map(tuple => {
      val word = tuple._1
      val ws = tuple._2

      //获取单词数量
      val count = ws.length

      word + "\t" + count
    })

    countList.foreach(println)


    Source
      .fromFile("java/data/words.txt") //读取文件
      .getLines() //获取所有数据
      .toList //将迭代器转换成list
      .flatMap(_.split(",")) //扁平化 一行变多行
      .groupBy(word => word) //分组  指定分组的字段
      .map(t => t._1 + "\t" + t._2.length) //统计单词数量
      .foreach(println)


  }
}
